package com.employeemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.employeemanage.domain.bean.TblProduct;
import com.employeemanage.domain.resp.PageResult;
import com.employeemanage.domain.vo.ProductAddVo;
import com.employeemanage.domain.vo.ProductListVo;
import com.employeemanage.domain.vo.ProductUpdateVo;
import com.employeemanage.service.TblProductService;
import com.employeemanage.mapper.TblProductMapper;
import com.employeemanage.mapper.TblProductOrderMapper;
import com.employeemanage.domain.bean.TblProductOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author 郭可欣
* @description 针对表【tbl_product(商品表)】的数据库操作Service实现
* @createDate 2025-07-05 14:20:29
*/
@Service
public class TblProductServiceImpl extends ServiceImpl<TblProductMapper, TblProduct>
    implements TblProductService{

    @Autowired
    private TblProductOrderMapper productOrderMapper;

    @Override
    public PageResult<TblProduct> getProductList(ProductListVo productListVo) {
        // 创建分页对象
        Page<TblProduct> page = new Page<>(productListVo.getPageNum(), productListVo.getPageSize());
        
        // 创建查询条件
        LambdaQueryWrapper<TblProduct> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(productListVo.getProductName())) {
            queryWrapper.like(TblProduct::getName, productListVo.getProductName());
        }
        if (StringUtils.hasText(productListVo.getCategory())) {
            queryWrapper.eq(TblProduct::getCategory, productListVo.getCategory());
        }
        if (StringUtils.hasText(productListVo.getBrand())) {
            queryWrapper.like(TblProduct::getBrand, productListVo.getBrand());
        }
        if (productListVo.getMinStock() != null) {
            queryWrapper.ge(TblProduct::getStock, productListVo.getMinStock());
        }
        if (productListVo.getMaxStock() != null) {
            queryWrapper.le(TblProduct::getStock, productListVo.getMaxStock());
        }
        
        // 按ID倒序排列
        queryWrapper.orderByDesc(TblProduct::getId);
        
        // 执行分页查询
        Page<TblProduct> resultPage = this.page(page, queryWrapper);
        
        return new PageResult<>(resultPage);
    }

    @Override
    public Long addProduct(ProductAddVo productAddVo) {
        TblProduct product = new TblProduct();
        BeanUtils.copyProperties(productAddVo, product);
        
        // 设置保质期
        if (StringUtils.hasText(productAddVo.getExpiryDate())) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date expiryDate = sdf.parse(productAddVo.getExpiryDate());
                product.setExpiryDate(expiryDate);
            } catch (ParseException e) {
                throw new RuntimeException("保质期格式错误，请使用yyyy-MM-dd格式");
            }
        }
        
        // 设置创建时间和更新时间
        Date now = new Date();
        product.setCreatedAt(now);
        product.setUpdatedAt(now);
        
        // 保存商品
        this.save(product);
        
        return product.getId();
    }

    @Override
    public boolean updateProduct(ProductUpdateVo productUpdateVo) {
        // 检查商品是否存在
        TblProduct existingProduct = this.getById(productUpdateVo.getId());
        if (existingProduct == null) {
            return false;
        }
        
        // 更新商品信息
        BeanUtils.copyProperties(productUpdateVo, existingProduct);
        
        // 设置保质期
        if (StringUtils.hasText(productUpdateVo.getExpiryDate())) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date expiryDate = sdf.parse(productUpdateVo.getExpiryDate());
                existingProduct.setExpiryDate(expiryDate);
            } catch (ParseException e) {
                throw new RuntimeException("保质期格式错误，请使用yyyy-MM-dd格式");
            }
        }
        
        // 设置更新时间
        existingProduct.setUpdatedAt(new Date());
        
        // 更新商品
        return this.updateById(existingProduct);
    }

    @Override
    public boolean deleteProduct(Long id) {
        // 检查商品是否存在
        TblProduct product = this.getById(id);
        if (product == null) {
            return false;
        }
        // 检查是否有关联订单
        Long orderCount = productOrderMapper.selectCount(
            new QueryWrapper<TblProductOrder>().eq("product_id", id)
        );
        if (orderCount != null && orderCount > 0) {
            throw new RuntimeException("该商品有关联订单，无法删除");
        }
        // 删除商品
        return this.removeById(id);
    }
}




